import json

import requests
import coo
headers= coo.headers
cookies= coo.cookies
NET_SessionId= coo.cookies['ASP.NET_SessionId']
class zhdt:
    #基本信息
    def base_message(self):
        params = {
            'usertype': '2',
            'isMy': '0',
            'getType': 'total',
            'sys_terno_flag': '1',
        }
        from datetime import datetime

        # 获取今天的日期
        today = datetime.today().date()
        data = {
            'ispage': '1',
            'page': '1',
            'pagesize': '10000',
            'apply_date': '2024-01-02~{}'.format(today),
            'FlowSta': '',
            'search_key': '',
            'Yt_SessionId': NET_SessionId,
        }

        response = requests.post(
            'http://59.215.206.249/zwfw/sqAction/getDataList.do',
            params=params,
            headers=headers,
            cookies=cookies,
            data=data,
            verify=False,
        ).json()
        list=[]
        types={
            1:'各级实体政务大厅咨询、投诉窗口',
            2:'办不成事”反映窗口',
            3:'综合受理窗口',
            4:'贵州政务服务网“盯事在线”、咨询投诉专区',
            5:'12345政务服务便民热线',
            6:'企业之家专区',
            7:'云坐席专区',
            8:'全省政务服务第三方舆情监测平台',
            9:'其他'
        }
        # print(response)
        for i in response['data']:
            try:
                if '5203' in i['areacode']:
                    #print(json.dumps(i, ensure_ascii=False, indent=4))
                    工单编号 = i['ID']
                    当前流程 = i['FlowStaName']
                    诉求人姓名=i['apply_user_xname']
                    # print(诉求人姓名)
                    诉求人电话=i['ApplicanterTel']
                    诉求人身份证=i['ApplicanterIDC']
                    诉求发生地=i['ApplicanterAreaName']
                    事件发生时间=i['apply_date']
                    登记人姓名=i['create_user_name']
                    登记人联系电话=i['registrantTel']
                    来源= types[i['source']]
                    类型=i['Type']
                    事件=i['event']
                    诉求描述=i['Title']
                    FLowId	=i['FLowId']
                    #  调用获取环节信息
                    环节信息=self.link_message(FLowId)
                    list.extend([[工单编号,当前流程,诉求人姓名,诉求人电话,诉求人身份证,诉求发生地,事件发生时间,来源,登记人姓名,登记人联系电话,类型,事件,诉求描述,环节信息]])

            except Exception as e:
                print(e)
        print(list)
        return list

    #环节信息
    def link_message(self,fdid):
        params = {
            'fdid': fdid,
            'sys_terno_flag': '1',
        }

        data = {
            'sys_terno_flag': '1',
            'Yt_SessionId': NET_SessionId,
        }

        response = requests.post('http://59.215.206.249/hyy_nflow/findLog.do', params=params, cookies=cookies,headers=headers, data=data).json()
        # print(response.text)
        sorted_data = sorted(response['data'], key=lambda x: x["lid"])

        # 更新原始数据字典中的 data 字段
        data["data"] = sorted_data
        datas=str(json.dumps(data['data']))
        data = json.loads(datas)

        # 格式化输出
        formatted_json = json.dumps(data, ensure_ascii=False, indent=2)
        list=[]
        print()
        for item in json.loads(formatted_json):
            combined_string = item['optusername'] +"->"+ item['statusName'] +"->"+ item['title'] +"->"+ item['applyAgree']
            list.append(combined_string)
        return list


# zhdt().base_message()
# zhdt().link_message('987d1dd0ed5c477e86b7bc133e81610f')

